/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package businessLogic;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import persistence.*;

/**
 *
 * @author breinesg
 */
@Stateless
public class DataMgrBean implements DataMgr {
    @PersistenceContext
    private EntityManager em;

    @Override
    public void addClient(String firstName, String lastName, String adress, String email, String phone) {
         
         
         Client cl = new Client();
         cl.setFirstName(firstName);
         cl.setLastName(lastName);
         cl.setAddress(adress);
         cl.setEmail(email);
         cl.setPhone(phone);          
         em.persist(cl);            
        
    }    

            
    public Library createLibrary(String libraryName, String location) {
        Library lib = new Library();
        lib.setLastName(libraryName);
        lib.setLocation(location);
        em.persist(lib);
        return lib;
    }

    public Library getLibrary(String name) {
        try {
            return em.createNamedQuery("Library.findByLastName", Library.class)
                    .setParameter("lastName", name)
                    .getSingleResult();
        } catch ( NoResultException e) {
            
            return createLibrary(name, "Urstein, wo sonst");
        }
    }
    
    public Shelf getShelf(Library lib, int pos, int row, int col) {
        try {
            return em.createNamedQuery("Shelf.findShelf", Shelf.class)
                    .setParameter("library", lib)
                    .setParameter("position", pos)
                    .setParameter("row", row)
                    .setParameter("col", col)
                    .getSingleResult();
        } catch( NoResultException e ) {
            Shelf s = new Shelf();
            s.setLibraryId(lib);
            s.setPosition(pos);
            s.setRow(row);
            s.setCol(col);
            em.persist(s);
            return s;
        }
    }
    

    
    public void addBook(String title, String isbn, String author) {
        Library lib = getLibrary("ITS");
        Shelf s = getShelf(lib, 1, 1, 1);
        
        SimpleBook book = new SimpleBook();
        book.setTitle(title);
        book.setIsbn(isbn);
        book.setAuthor(author);
        book.setShelfId(s);
        em.persist(book);
    }
    
        public void addCD(String title, String interpret, String record) {
        Library lib = getLibrary("ITS");
        Shelf s = getShelf(lib, 2, 1, 1);
        
        MusikCd cd= new MusikCd();
        cd.setTitle(title);
        cd.setInterpret(interpret);
        cd.setRecord(record);
        cd.setShelfId(s);
        em.persist(cd);
    }
    
    
 
  
    @Override
    public List<Medium> getAllMedia() {
        
        return em.createNamedQuery("Medium.findAll").getResultList();
    }
    
        
     @Override
    public List<Client> getAllClients() {
        
        return em.createNamedQuery("Client.findAll").getResultList();
    }


}
